// pkgTask/pages/prebid-communication/index.js
const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
const operateMethod =require('../service/operatef.js');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        interactOption: [],
        condition: '',
        isArriveScene: 1,
        interactList: [{}],
        columnsItemThings: [],
        showInteractPerson: false,
        userList: [],
        reasonList: [],
        reason: '',
        friendlyList: [{}]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        that.setData({
            taskId: options.taskId,
            operationType: options.type,
            hospitalId: options.hospitalId,
            operationlist: options.operationlist ? JSON.parse(options.operationlist) : [],
            operationTypeCode: options.operationTypeCode,
            activeKey: options.activeKey
        })
        that.getDetailById()
        that.getProjectList('link_up')
        that.getProjectList('causes_blindness')
        that.getProjectList('longTask_communication')
        that.getFriendlyCommunicate()
    },
    //获取任务详情
    getDetailById() {
        let that = this
        lbbTaskService.getLongDetailById({
            taskId: that.data.taskId
        }, function (res) {
            if (res.data) {
                that.setData({
                    taskProjectText: res.data.taskProjectText,
                    taskClassificationName: res.data.taskClassificationName,
                    hospitalName: res.data.hospitalName,
                    taskDesc: res.data.taskDesc,
                    taskImages: res.data.taskImages ? res.data.taskImages.split(',') : [],
                    isUrgent: res.data.isUrgent
                })
            }
        })
    },
    //获取数据字典值
    getProjectList(code) {
        let that = this
        lbbTaskService.getDictByCode({
            code: code
        }, function (res) {
            console.log(res)
            if (code == 'longTask_communication') {
                that.setData({
                    columnsItemThings: res.data
                })
            } else if (code == 'link_up') {
                res.data.unshift({
                    text: '请选择',
                    value: ''
                })
                that.setData({
                    interactOption: res.data
                })
            } else if (code == 'causes_blindness') {
                res.data.unshift({
                    text: '请选择',
                    value: ''
                })
                that.setData({
                    reasonList: res.data
                })
            }
        })
    },
    //回显
    getFriendlyCommunicate() {
        let that = this
        lbbTaskService.getFriendlyCommunicate({
            taskId: that.data.taskId,
        }, function (res) {
            console.log(777, res.data)
            if (res.data) {
                res.data.forEach(item => {
                    if(!item.communicationInfo){
                        item.communicationInfo={}
                    }else{
                        item.communicationInfo.remark = item.communicationInfo.remark ? item.communicationInfo.remark : ''
                        item.communicationInfo.images = item.communicationInfo.images ? item.communicationInfo.images.split(',') : ''
                    }
                    if (item.communicationInfo.conditionList) {
                        item.communicationInfo.conditionList?.forEach(itm => {
                            itm.remark = itm.remark ? itm.remark : ''
                            itm.images = itm.images ? itm.images.split(',') : ''
                            itm.username = item.contactList.find(items => items.value == itm.userId)?.label
                            itm.matterInfoText = that.data.columnsItemThings.find(items => items.value == itm.matterInfo)?.label
                        })
                    } else {
                        item.communicationInfo.conditionList = [{}]
                    }
                })
                that.setData({
                    friendlyList: res.data
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    //沟通情况选择回调
    onChangeInteract(e) {
        this.data.friendlyList[e.currentTarget.dataset.index].communicationInfo.condition = e.detail
        this.setData({
            friendlyList: this.data.friendlyList
        })
        console.log(33,this.data.friendlyList)
    },
    //是否到现场选择回调
    onChangeRadio(e) {
        this.data.friendlyList[e.currentTarget.dataset.index].communicationInfo.isArriveScene = e.detail
        this.setData({
            friendlyList: this.data.friendlyList
        })
    },
    //打开沟通人弹窗
    onBindInteractPerson(e) {
        this.setData({
            showInteractPerson: true,
            optionIndex: e.currentTarget.dataset.index,
            itemIndex: e.currentTarget.dataset.indext,
            userList: this.data.friendlyList[e.currentTarget.dataset.index].contactList
        })
    },
    //选择人回调
    onClickItem(e) {
        console.log(555, e)
        this.data.friendlyList[this.data.optionIndex].communicationInfo.conditionList[this.data.itemIndex].username = e.detail.name
        this.data.friendlyList[this.data.optionIndex].communicationInfo.conditionList[this.data.itemIndex].userId = e.detail.id
        this.setData({
            showInteractPerson: false,
            friendlyList: this.data.friendlyList
        })
    },
    //打开沟通事项
    onBindItemThings(e) {
        console.log(e)
        this.setData({
            showItemThings: true,
            optionIndex: e.currentTarget.dataset.index,
            itemIndex: e.currentTarget.dataset.indext
        })
    },
    //沟通事项选择回调
    okSelect(e) {
        console.log(11, e)
        this.data.friendlyList[this.data.optionIndex].communicationInfo.conditionList[this.data.itemIndex].matterInfoText = e.detail.text
        this.data.friendlyList[this.data.optionIndex].communicationInfo.conditionList[this.data.itemIndex].matterInfo = e.detail.value
        this.setData({
            friendlyList: this.data.friendlyList
        })
    },
    //备注
    changeRemark(e) {
        this.setData({
            optionIndex: e.currentTarget.dataset.index,
            itemIndex: e.currentTarget.dataset.indext
        })
        this.data.friendlyList[this.data.optionIndex].communicationInfo.conditionList[this.data.itemIndex].remark = e.detail.value
        this.setData({
            friendlyList: this.data.friendlyList
        })
    },
    //图片上传
    setImgArr(e) {
        let that = this
        let imgArray = e.detail;
        let newUpLoadImg = []
        let oldUpLoadImg = []
        imgArray.forEach(item => {
            if (item.indexOf('http://tmp/') != -1 || item.indexOf('wxfile://tmp') != -1) {
                newUpLoadImg.push(item)
            } else {
                oldUpLoadImg.push(item)
            }
        })
        wx.showLoading({
            title: '正在上传图片',
            mask: true
        })
        util.newUpLoadImg(app.globalData.header, newUpLoadImg, 0, [], function (imgIdArray) {
            console.log('item================>', imgIdArray)
            wx.hideLoading()
            oldUpLoadImg.push(...imgIdArray)
            that.data.friendlyList[e.currentTarget.dataset.index].communicationInfo.conditionList[e.currentTarget.dataset.indext].images = oldUpLoadImg
        });
    },
    //删除
    delTask(e) {
        this.data.friendlyList[e.currentTarget.dataset.index].communicationInfo.conditionList.splice(e.currentTarget.dataset.indext,1)
        this.setData({
            friendlyList: this.data.friendlyList
        })
    },
    //添加
    addTask(e) {
        this.data.friendlyList[e.currentTarget.dataset.index].communicationInfo.conditionList.push({})
        this.setData({
            friendlyList: this.data.friendlyList
        })
    },
    //选择未沟通原因
    onChangeReason(e) {
        this.data.friendlyList[e.currentTarget.dataset.index].communicationInfo.reason = e.detail
        this.setData({
            friendlyList: this.data.friendlyList
        })
    },
    //未沟通备注
    changeRemarkNot(e) {
        this.data.friendlyList[e.currentTarget.dataset.index].communicationInfo.remark = e.detail.value
        this.setData({
            friendlyList: this.data.friendlyList
        })
    },
    //未沟通图片
    setImgArrNot(e) {
        let that = this
        let imgArray = e.detail;
        let newUpLoadImg = []
        let oldUpLoadImg = []
        imgArray.forEach(item => {
            if (item.indexOf('http://tmp/') != -1 || item.indexOf('wxfile://tmp') != -1) {
                newUpLoadImg.push(item)
            } else {
                oldUpLoadImg.push(item)
            }
        })
        wx.showLoading({
            title: '正在上传图片',
            mask: true
        })
        util.newUpLoadImg(app.globalData.header, newUpLoadImg, 0, [], function (imgIdArray) {
            console.log('item================>', imgIdArray)
            wx.hideLoading()
            oldUpLoadImg.push(...imgIdArray)
            this.data.friendlyList[e.currentTarget.dataset.index].communicationInfo.images = oldUpLoadImg
        });
    },
    //提交
    btnSubmit() {
        let that = this
        let checkList = []
        that.data.friendlyList.forEach(item => {
            if (item.communicationInfo.condition == 0) {
                item.conditionList = []
                item.remark = item.remark ? item.remark : ''
                item.images = item.images ? item.images.toString() : ''
                if (item.communicationInfo.isArriveScene != 0 && item.communicationInfo.isArriveScene != 1) {
                    checkList.push(0)
                } else {
                    checkList.push(1)
                }
                if (!item.communicationInfo.reason && item.communicationInfo.reason !== 0) {
                    checkList.push(0)
                } else {
                    checkList.push(1)
                }
            } else if (item.communicationInfo.condition == 1) {
                item.communicationInfo.conditionList.forEach(itm => {
                    itm.images = itm.images ? itm.images.toString() : ''
                        itm.remark = itm.remark ? itm.remark : ''
                    if (!itm.userId) {
                        checkList.push(0)
                    } else {
                        checkList.push(1)
                    }
                    if (!itm.matterInfo && itm.matterInfo != 0) {
                        checkList.push(0)
                    } else {
                        checkList.push(1)
                    }
                })
            }
        })
        if (checkList.indexOf(0) != -1 || checkList.length == 0) {
            that.openPrompt()
        } else {
            lbbTaskService.friendlyCommunicate({
                friendlyInfo: JSON.stringify({
                    friendlyList: that.data.friendlyList,
                    taskId: that.data.taskId
                })
            }, function (res) {
                if (that.data.activeKey == 3) {
                    wx.navigateBack({
                        delta: 1
                    })
                } else {
                    operateMethod(that.data.operationlist, that.data.taskId, that.data.hospitalId, that.data.taskType, that.data.operationTypeCode)
                }
            })
        }

    },
    //校验提示
    openPrompt() {
        wx.showToast({
            title: '请检查必填项',
            icon: 'error',
            duration: 2000
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})